package com.firebird.app.fejpsl.adapter

import android.content.Context
import android.text.Html
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.firebird.app.fejpsl.R
import com.firebird.app.fejpsl.mvp.model.bean.Article
import com.firebird.app.fejpsl.utils.ImageLoader
import kotlinx.android.synthetic.main.item_home_list.view.*

/**
 * Created by firebird on 2018/4/22.
 */
class HomeAdapter(private val context: Context?, datas: MutableList<Article>)
    : BaseQuickAdapter<Article, BaseViewHolder>(R.layout.item_home_list, datas) {

    override fun convert(holder: BaseViewHolder, item: Article) {
        item ?: return
        holder ?: return
        val authorStr = if (item.author.isNotEmpty()) item.author else item.share_user

        holder.setText(R.id.tv_article_title, Html.fromHtml(item.title))
                .setText(R.id.tv_article_author, authorStr)
                .setText(R.id.tv_article_date, item.nice_date)
                .setImageResource(R.id.iv_like,
                        if (item.item_collect) R.drawable.ic_like else R.drawable.ic_like_not
                )
                .addOnClickListener(R.id.iv_article_thumbnail)
        val chapterName = when {
            item.super_chapter_name.isNotEmpty() and item.chapter_name.isNotEmpty() ->
                "${item.super_chapter_name} / ${item.chapter_name}"
            item.super_chapter_name.isNotEmpty() -> item.super_chapter_name
            item.chapter_name.isNotEmpty() -> item.chapter_name
            else -> ""
        }
        holder.setText(R.id.tv_article_chapterName, chapterName)
        if (item.envelope_pic.isNotEmpty()) {
            holder.getView<ImageView>(R.id.iv_article_thumbnail)
                    .visibility = View.VISIBLE
            context?.let {
                ImageLoader.load(it, item.envelope_pic, holder.getView(R.id.iv_article_thumbnail))
            }
        } else {
            holder.getView<ImageView>(R.id.iv_article_thumbnail)
                    .visibility = View.GONE
        }
        val tv_fresh = holder.getView<TextView>(R.id.tv_article_fresh)
        if (item.fresh) {
            tv_fresh.visibility = View.VISIBLE
        } else {
            tv_fresh.visibility = View.GONE
        }
        val tv_top = holder.getView<TextView>(R.id.tv_article_top)
        if (item.top == "1") {
            tv_top.visibility = View.VISIBLE
        } else {
            tv_top.visibility = View.GONE
        }
        val tv_article_tag = holder.getView<TextView>(R.id.tv_article_tag)

        tv_article_tag.visibility=View.GONE
    }

}
